package com.weed.client.component.ext;


import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.border.AbstractBorder;

public class ImageBorder extends AbstractBorder {
    private Image top_center;
    private Image top_left;
    private Image top_right;
    private Image left_center; 
    private Image right_center;
    private Image bottom_center;
    private Image bottom_left;
    private Image bottom_right;
    Insets insets;
     
    public ImageBorder(Image top_left, Image top_center, Image top_right,
        Image left_center, Image right_center,
        Image bottom_left, Image bottom_center, Image bottom_right) {
            
        this.top_left = top_left;
        this.top_center = top_center;
        this.top_right = top_right;
        this.left_center = left_center;
        this.right_center = right_center;
        this.bottom_left = bottom_left;
        this.bottom_center = bottom_center;
        this.bottom_right = bottom_right;
         
    }
    
    public void setInsets(Insets insets) {
        this.insets = insets;
    }
    
    public Insets getBorderInsets(Component c) {
        if(insets != null) {
            return insets;
        } else {
            return new Insets(0,0,0,0);
            //return new Insets(getTop_center().getHeight(null),getLeft_center().getWidth(null),
            //    getBottom_center().getHeight(null), getRight_center().getWidth(null));
        }
    }
    
    
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        g.setColor(Color.white);
        g.fillRect(x,y,width,height);

        Graphics2D g2 = (Graphics2D)g;
         int tlw = getImageWidth(getTop_left());
        int tlh = getImageHeight(getTop_left());
        int tcw = getImageWidth(getTop_center());
        int tch = getImageHeight(getTop_center());
        int trw = getImageWidth(getTop_right());
        int trh = getImageHeight(getTop_right());

        int lcw = getImageWidth(getLeft_center());
        int lch = getImageHeight(getLeft_center());
        int rcw = getImageWidth(getRight_center());
        int rch = getImageHeight(getRight_center());

        int blw = getImageWidth(getBottom_left());
        int blh = getImageHeight(getBottom_left());
        int bcw = getImageWidth(getBottom_center());
        int bch = getImageHeight(getBottom_center());
        int brw = getImageWidth(getBottom_right());
        int brh = getImageHeight(getBottom_right());
       
        
        fillTexture(g2, getTop_left(),x,      y,
          tlw,            tlh);
        fillTexture(g2, getTop_center(),    x+tlw,
          y,            width-tlw-trw, tch);
        fillTexture(g2, getTop_right(),     x+width-trw,  y,            trw,           trh);
        
        fillTexture(g2, getLeft_center(),x,            y+tlh,        lcw,           height-tlh-blh);
        fillTexture(g2, getRight_center(),  x+width-rcw,  y+trh,        rcw,           height-trh-brh);
        
        fillTexture(g2, getBottom_left(),x,            y+height-blh, blw,           blh);
        fillTexture(g2, getBottom_center(), x+blw,        y+height-bch, width-blw-brw, bch);
        fillTexture(g2, getBottom_right(),  x+width-brw,  y+height-brh, brw,           brh);
        
    }
    
    public void fillTexture(Graphics2D g2, Image img, int x, int y, int w, int h) {
        if(img!=null){
            BufferedImage buff = createBufferedImage(img);
            Rectangle anchor = new Rectangle(x,y,
                img.getWidth(null),img.getHeight(null));
            TexturePaint paint = new TexturePaint(buff,anchor);
            g2.setPaint(paint);
            g2.fillRect(x,y,w,h);
        }
    }
    protected int getImageWidth(Image image){
        if(image == null)return 0;
        return image.getWidth(null);
    }
    protected int getImageHeight(Image image){
        if(image == null)return 0;
        return image.getHeight(null); 
    }
    public BufferedImage createBufferedImage(Image img) {
        BufferedImage buff = new BufferedImage(img.getWidth(null), 
            img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics gfx = buff.createGraphics();
        gfx.drawImage(img, 0, 0, null);
        gfx.dispose();
        return buff;
    }

    /**
     * @return the top_center
     */
    public Image getTop_center() {
        return top_center;
    }

    /**
     * @param top_center the top_center to set
     */
    public void setTop_center(Image top_center) {
        this.top_center = top_center;
    }

    /**
     * @return the top_left
     */
    public Image getTop_left() {
        return top_left;
    }

    /**
     * @param top_left the top_left to set
     */
    public void setTop_left(Image top_left) {
        this.top_left = top_left;
    }

    /**
     * @return the top_right
     */
    public Image getTop_right() {
        return top_right;
    }

    /**
     * @param top_right the top_right to set
     */
    public void setTop_right(Image top_right) {
        this.top_right = top_right;
    }

    /**
     * @return the left_center
     */
    public Image getLeft_center() {
        return left_center;
    }

    /**
     * @param left_center the left_center to set
     */
    public void setLeft_center(Image left_center) {
        this.left_center = left_center;
    }

    /**
     * @return the right_center
     */
    public Image getRight_center() {
        return right_center;
    }

    /**
     * @param right_center the right_center to set
     */
    public void setRight_center(Image right_center) {
        this.right_center = right_center;
    }

    /**
     * @return the bottom_center
     */
    public Image getBottom_center() {
        return bottom_center;
    }

    /**
     * @param bottom_center the bottom_center to set
     */
    public void setBottom_center(Image bottom_center) {
        this.bottom_center = bottom_center;
    }

    /**
     * @return the bottom_left
     */
    public Image getBottom_left() {
        return bottom_left;
    }

    /**
     * @param bottom_left the bottom_left to set
     */
    public void setBottom_left(Image bottom_left) {
        this.bottom_left = bottom_left;
    }

    /**
     * @return the bottom_right
     */
    public Image getBottom_right() {
        return bottom_right;
    }

    /**
     * @param bottom_right the bottom_right to set
     */
    public void setBottom_right(Image bottom_right) {
        this.bottom_right = bottom_right;
    }
}

